package com.ihk.model;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

public class Parser{
	
	private static Parser instance;
	private static ArrayList<Toilet> toilets;

	private static int maxId = 0;
	private static String path = Environment.getExternalStorageDirectory().getPath()+"/toiletfinder/";
	private static String filePath = path + "kobenhavn.xml";

	public static ArrayList<Toilet> getToilets() {
		return toilets;
	}

	public static void setToilets(ArrayList<Toilet> toilets) {
		Parser.toilets = toilets;
	}
	
	public static String getFilePath() {
		return filePath;
	}

	public static String getPath() {
		return path;
	}

	public static int getMaxId() {
		return maxId;
	}

	private Parser() {
	}
	
	public static Parser getInstance() {
		if(instance == null) {
			instance = new Parser();
		}
		
		return instance;
	}
	
	public void parse(InputStream inputStream) throws Exception{
		XmlPullParser parser =Xml.newPullParser();
		parser.setInput(inputStream, "UTF-8");
		
		toilets = new ArrayList<Toilet>();

		Toilet toilet = null;
		int eventType = parser.getEventType();

		while(eventType != XmlPullParser.END_DOCUMENT){
			if(eventType == XmlPullParser.START_TAG){
				String tag = parser.getName();
				if("node".equals(tag)){
					toilet = new Toilet();
					toilets.add(toilet);
				}
				if("nid".equals(tag)){
					toilet.nid = Integer.parseInt(parser.nextText());
					if(toilet.nid > maxId){
						maxId = toilet.nid;
					}
				}
				if ("Street".equals(tag)) {
					toilet.street = parser.nextText();
				}
				if ("Postalcode".equals(tag)) {
					toilet.postalCode = Integer.parseInt(parser.nextText());
				}
				if ("City".equals(tag)) {
					toilet.city = parser.nextText();
				}
				if ("Latitude".equals(tag)) {
					toilet.latitude = Float.parseFloat(parser.nextText());
				}
				if ("Longitude".equals(tag)) {
					toilet.longitude = Float.parseFloat(parser.nextText());
				}
				if ("type".equals(tag)) {
					toilet.type = parser.nextText();
				}
				if ("Betaling".equals(tag)) {
					toilet.betaling = parser.nextText();
				}
				if ("Bemanding".equals(tag)) {
					toilet.bemanding = parser.nextText();
				}
				if ("Kanylebeholder".equals(tag)) {
					toilet.kanylebeholder = parser.nextText();
				}
				if ("Puslebord".equals(tag)) {
					toilet.puslebord = parser.nextText();
				}
				if ("Vandhane".equals(tag)) {
					toilet.vandhane = parser.nextText();
				}
				if ("FriGulvplads".equals(tag)) {
					toilet.friGulvplads = Integer.parseInt(parser.nextText());
				}
				if ("Body".equals(tag)) {
					toilet.body = parser.nextText();
				}
			}
			eventType = parser.next();
		}
	}
	
	public static void addNewToilet(Toilet toilet) {
		toilets.add(toilet);
	}
	
	public static void saveToiletsAsXML() {
		
    	try {
			BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(Parser.getFilePath()));
			buf.write(writeXml().getBytes());
			buf.close();
    	}
    	catch (IOException e) {
			Log.d("ToXMLErrors", e.getMessage());
    	}
	}
	
	private static String writeXml(){
	    XmlSerializer serializer = Xml.newSerializer();
	    StringWriter writer = new StringWriter();
	    try {
	        serializer.setOutput(writer);
	        serializer.startDocument("UTF-8", true);
        	serializer.startTag("", "xml");
	        for (Toilet toilet: toilets){
	        	serializer.startTag("", "node");
	        	serializer.startTag("", "nid");
	            serializer.text(""+toilet.nid);
	            serializer.endTag("", "nid");
	        	serializer.startTag("", "Street");
	            serializer.text(toilet.street);
	            serializer.endTag("", "Street");
	        	serializer.startTag("", "Postalcode");
	            serializer.text(""+toilet.postalCode);
	            serializer.endTag("", "Postalcode");
	        	serializer.startTag("", "City");
	            serializer.text(toilet.city);
	            serializer.endTag("", "City");
	        	serializer.startTag("", "Latitude");
	            serializer.text(""+toilet.latitude);
	            serializer.endTag("", "Latitude");
	        	serializer.startTag("", "Longitude");
	            serializer.text(""+toilet.longitude);
	            serializer.endTag("", "Longitude");
	        	serializer.startTag("", "type");
	            serializer.text(toilet.type);
	            serializer.endTag("", "type");
	        	serializer.startTag("", "Betaling");
	            serializer.text(toilet.betaling);
	            serializer.endTag("", "Betaling");
	        	serializer.startTag("", "Bemanding");
	            serializer.text(toilet.bemanding);
	            serializer.endTag("", "Bemanding");
	        	serializer.startTag("", "Kanylebeholder");
	            serializer.text(toilet.kanylebeholder);
	            serializer.endTag("", "Kanylebeholder");
	        	serializer.startTag("", "Puslebord");
	            serializer.text(toilet.puslebord);
	            serializer.endTag("", "Puslebord");
	        	serializer.startTag("", "Vandhane");
	            serializer.text(toilet.vandhane);
	            serializer.endTag("", "Vandhane");
	        	serializer.startTag("", "FriGulvplads");
	            serializer.text(""+toilet.friGulvplads);
	            serializer.endTag("", "FriGulvplads");
	        	serializer.startTag("", "Body");
	            serializer.text(toilet.body);
	            serializer.endTag("", "Body");
	            serializer.endTag("", "node");
	        }
            serializer.endTag("", "xml");
	        serializer.endDocument();
	        return writer.toString();
	    } catch (Exception e) {
	        throw new RuntimeException(e);
	    } 
	}
}